/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Message.java
 *
 * Created on 2011-03-22, 17:21:47
 */

package trumpet.gui;

import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import trumpet.connection.Connection;
import trumpet.core.User;

/**
 *
 * @author michal
 */
public class Message extends javax.swing.JFrame {

    public static final String PERSON_ME = "ja";
    
    protected Connection _connection;
    protected User _user;
    
    /** Creates new form Message */
    public Message(User user) {
        _user = user;
        initComponents();
        setTitle("Rozmowa z " + _user.getFullName());
    }

    public void setConnection(Connection c)
    {
        _connection = c;
    }
    
    public Connection getConnection()
    {
        return _connection;
    }
    
    protected void addMessage(String who, String message)
    {
        DefaultListModel model = (DefaultListModel)MessagesList.getModel();
        model.addElement(who + ": " + message);
    }
    
    public void sendMessage(String message)
    {
        addMessage(_user.getFullName(), message);
    }
    
    public void setVideoComponent(Component component)
    {
        System.out.println(component);
        VideoPanel.add(component);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MessageInputField = new javax.swing.JTextField();
        MainPanel = new javax.swing.JSplitPane();
        MessagesScroll = new javax.swing.JScrollPane();
        MessagesList = new javax.swing.JList();
        VideoPanel = new javax.swing.JPanel();
        SendVideojButton = new javax.swing.JButton();

        setTitle("Rozmowa z...");

        MessageInputField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                MessageInputFieldKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                MessageInputFieldKeyTyped(evt);
            }
        });

        MainPanel.setDividerLocation(250);

        MessagesScroll.setPreferredSize(new java.awt.Dimension(300, 100));

        MessagesList.setModel(new DefaultListModel());
        MessagesScroll.setViewportView(MessagesList);

        MainPanel.setLeftComponent(MessagesScroll);

        VideoPanel.setBackground(new java.awt.Color(1, 1, 1));

        SendVideojButton.setText("Wyslij wideo");
        SendVideojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendVideojButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout VideoPanelLayout = new javax.swing.GroupLayout(VideoPanel);
        VideoPanel.setLayout(VideoPanelLayout);
        VideoPanelLayout.setHorizontalGroup(
            VideoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(VideoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SendVideojButton)
                .addContainerGap(272, Short.MAX_VALUE))
        );
        VideoPanelLayout.setVerticalGroup(
            VideoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, VideoPanelLayout.createSequentialGroup()
                .addContainerGap(344, Short.MAX_VALUE)
                .addComponent(SendVideojButton)
                .addContainerGap())
        );

        MainPanel.setRightComponent(VideoPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(MessageInputField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
            .addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(MessageInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MessageInputFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_MessageInputFieldKeyPressed
        if (evt.getKeyChar() == '\n') {
            String message = MessageInputField.getText();
            MessageInputField.setText(null);
            
            getConnection().sendMessage(message);
            addMessage(PERSON_ME, message);
        }
    }//GEN-LAST:event_MessageInputFieldKeyPressed

    private void MessageInputFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_MessageInputFieldKeyTyped
        
    }//GEN-LAST:event_MessageInputFieldKeyTyped

    private void SendVideojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendVideojButtonActionPerformed
        _connection.sendVideoStream();
        JButton b = (JButton)evt.getSource();
        b.setVisible(false);
    }//GEN-LAST:event_SendVideojButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSplitPane MainPanel;
    private javax.swing.JTextField MessageInputField;
    private javax.swing.JList MessagesList;
    private javax.swing.JScrollPane MessagesScroll;
    private javax.swing.JButton SendVideojButton;
    private javax.swing.JPanel VideoPanel;
    // End of variables declaration//GEN-END:variables

}
